package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.Update;
import com.heima.behavior.dto.ArticleDto;
import com.heima.behavior.dto.LikesBehaviorDto;
import com.heima.behavior.entity.ApArticle;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.feign.ApArticleController;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApLikesBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * APP点赞行为表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-01-18
 */
@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements IApLikesBehaviorService {

    @Autowired
    IApBehaviorEntryService entryService;

    @Autowired
    private ApArticleController apArticleFeign;

    //保存点赞操作
    @Override
    public ResponseResult getLike(LikesBehaviorDto dto) {

        //1 创建点赞行为数据对象
        ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
        //2 获取用户
        User user = AppThreadLocalUtil.get();

        //3 用户id不为null
        if (user != null) {

            Integer userId = user.getUserId();

            LambdaQueryWrapper<ApBehaviorEntry> query = new LambdaQueryWrapper<>();

            query.eq(ApBehaviorEntry::getEntryId, userId.toString());

            query.eq(ApBehaviorEntry::getType, 1);
            //3.1 查询行为实体获取实体ID
            ApBehaviorEntry one = entryService.getOne(query);
            //3.2 为点赞行为设置值
            apLikesBehavior.setEntryId(one.getId());

        } else {

            // 4 用户id为0的时候,查询行为实体，获取实体id
            LambdaQueryWrapper<ApBehaviorEntry> query = new LambdaQueryWrapper<>();

            query.eq(ApBehaviorEntry::getEntryId, dto.getEquipmentId());

            query.eq(ApBehaviorEntry::getType, 0);

            ApBehaviorEntry one = entryService.getOne(query);
            //4.1 为点赞行为设置实体id属性值
            apLikesBehavior.setEntryId(one.getId());
        }

        //为点赞行为赋值
        apLikesBehavior.setCreatedTime(new Date());
        //为点赞行为赋值
        apLikesBehavior.setOperation(dto.getOperation());
        //为点赞行为赋值
        apLikesBehavior.setType(dto.getType());
        //为点赞行为赋值
        apLikesBehavior.setArticleId(dto.getArticleId());
        //5 保存点赞行为
        this.save(apLikesBehavior);
        //6 远程调用文章接口，为ap_article的点赞数+1
        ApArticle apArticle = apArticleFeign.selectArticleById(dto.getArticleId());
        // 6.1 获取点赞
        int likes = apArticle.getLikes();
        // 6.2 加1
        Integer i = ++likes;

        apArticle.setLikes(i);
        // 7 远程调用feign接口保存点赞行为
        ResponseResult responseResult = apArticleFeign.selectArticleById(apArticle);

        // 8 响应数据到客户端
        if (responseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {

            return ResponseResult.okResult(apLikesBehavior.getId());
        }
        return null;
    }
}
